package com.ansion.basedb.database.sql;

import android.content.ContentValues;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ansion.utils.ValueSafeTransfer;

/**
 * created by zhaoyuntao
 * on 10/11/2023
 */
public class SQLRow {

    private final ContentValues contentValues;

    public SQLRow() {
        this.contentValues = new ContentValues();
    }

    public SQLRow put(@NonNull String key, @Nullable Object value) {
        if (value == null) {
            contentValues.putNull(key);
        } else if (value instanceof String) {
            contentValues.put(key, (String) value);
        } else if (value instanceof Byte) {
            contentValues.put(key, (Byte) value);
        } else if (value instanceof Short) {
            contentValues.put(key, (Short) value);
        } else if (value instanceof Integer) {
            contentValues.put(key, (Integer) value);
        } else if (value instanceof Long) {
            contentValues.put(key, (Long) value);
        } else if (value instanceof Float) {
            contentValues.put(key, (Float) value);
        } else if (value instanceof Double) {
            contentValues.put(key, (Double) value);
        } else if (value instanceof Boolean) {
            contentValues.put(key, (Boolean) value);
        } else if (value instanceof byte[]) {
            contentValues.put(key, (byte[]) value);
        } else {
            throw new IllegalArgumentException("Unsupported type " + value.getClass());
        }
        return this;
    }

    @NonNull
    public ContentValues getContentValues() {
        return contentValues;
    }

    public String getAsString(String key) {
        return contentValues.getAsString(key);
    }

    public Long getAsLong(String key) {
        return ValueSafeTransfer.value(contentValues.getAsLong(key), 0);
    }

    public Integer getAsInteger(String key) {
        return ValueSafeTransfer.value(contentValues.getAsInteger(key), 0);
    }

    public Short getAsShort(String key) {
        return ValueSafeTransfer.value(contentValues.getAsShort(key), (short) 0);
    }

    public Byte getAsByte(String key) {
        return ValueSafeTransfer.value(contentValues.getAsByte(key), (byte) 0);
    }

    public Double getAsDouble(String key) {
        return ValueSafeTransfer.value(contentValues.getAsDouble(key), 0);
    }

    public Float getAsFloat(String key) {
        return ValueSafeTransfer.value(contentValues.getAsFloat(key), 0);
    }

    public Boolean getAsBoolean(String key) {
        return ValueSafeTransfer.value(contentValues.getAsBoolean(key), false);
    }

    public byte[] getAsByteArray(String key) {
        return contentValues.getAsByteArray(key);
    }

    public int getCount() {
        return contentValues.getAsInteger(SQLColumn.NAME_COUNT);
    }

    @NonNull
    @Override
    public String toString() {
        return "SQLRow{" +
                ", contentValues=" + contentValues +
                '}';
    }

    public SQLRow put(ContentValues values) {
        this.contentValues.putAll(values);
        return this;
    }
}
